﻿using System;
using System.Collections.Generic;
using System.IO;
using Document.Library;
using Container.Library;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using Tool.Model;
using Web.Manage.App_Start;
using System.Text;
using Common.Library;

namespace Web.Manage.Areas.Staff.Controllers
{
    /// <summary>
    /// 员工考勤月报操作控制器
    /// </summary>
    [Area("Staff")]
    public class ExamineStatisticsController : BaseController
    {
        private IHostingEnvironment hostingEnv;

        private readonly IEmployeeService employeeService = null;//员工管理逻辑

        public ExamineStatisticsController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
            //员工管理逻辑注入
            employeeService = UnityCIContainer.Instance.GetService<IEmployeeService>();
        }


        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        #region 查询

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadExamineStatisticsList()
        {
            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<ExamineStatisticsQueryRequest>>();
             
                resultInfo = employeeService.LoadExamineStatisticsList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <returns>处理结果</returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult EpxortExamine()
        {
            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();

            FileStream fs = null;
            BinaryWriter bw = null;

            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<ExamineStatisticsQueryRequest>>();

                resultInfo = employeeService.LoadExamineStatisticsAllList(parametersInfo);

                List<string> headlist = new List<string>() { "员工编号", "姓名", "性别", "类别", "状态" };

                StringBuilder monthStr = new StringBuilder();
                var monthTimeLis = TimeHelper.GetMonthTimeList(parametersInfo.parameters.dBeginTime, parametersInfo.parameters.dEndTime);
                foreach (var item in monthTimeLis)
                {
                    headlist.Add(item);
                }
                headlist.Add("出勤合计");

                string fileName = "员工月考情信息-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

                var savePath = ExcelHelper.ExportToExcelReturnFilePath<Dictionary<string, object>>(resultInfo.Data, headlist,
                                   rowData =>
                                   {

                                       List<object> content = new List<object>();
                                       content.Add(rowData["UserNo"]);
                                       content.Add(rowData["Name"]);
                                       content.Add(EnumExtentions.GetEnumDescriptionByValue(int.Parse(rowData["Sex"].ToString()), typeof(SexType)));
                                       content.Add(rowData["MName"]);
                                       content.Add(rowData["ZTBS"]!=null&& rowData["ZTBS"].ToString().ToLower()=="true"? "在职":"离职");
                                       foreach (var item in monthTimeLis)
                                       {
                                           content.Add(rowData[item]);
                                       }
                                       content.Add(rowData["AttendanceTotal"]);

                                       return content.ToArray();
                                   },
                               false,
                               hostingEnv.WebRootPath,
                               fileName
                               );
                resultInfo.Msg = savePath;

            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            finally
            {
                if (fs != null) fs.Close();
                if (bw != null) bw.Close();
            }
            return ReturnContent(resultInfo);
        }

        #endregion
    }
}